package com.chen.controller;

import com.chen.base.utils.R;
import com.chen.dto.ClassDTO;
import com.chen.service.ClassesService;
import com.chen.service.GradeService;
import com.chen.service.TeacherService;
import com.chen.vo.ClassesVO;
import com.chen.vo.GradeVO;
import com.chen.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/classes")
public class ClassesController {

    @Autowired
    private ClassesService classesService;

    // 年级信息
    @Autowired
    private GradeService gradeService;

    // 教师信息
    @Autowired
    private TeacherService teacherService;

    /**
     * 某年级下的班级信息
     * @param gradeId
     * @throws IOException
     */
    @ResponseBody
    @GetMapping(value="/gradeclass/{gradeId}")
    public R<List<ClassesVO>> getClassesByGradeId(@PathVariable("gradeId") Integer gradeId) {
        List<ClassesVO> classes = classesService.findClassesByGradeId(gradeId);
        return R.<List<ClassesVO>>ok().data(classes);
    }


    /**
     * 进入到添加班级页面接口
     * @return
     */
    @GetMapping("/preAddClass")
    public ModelAndView preAddClass(ModelAndView model) {
        // 获取年级信息
        List<GradeVO> grades = gradeService.findAllGrade();
        model.setViewName("/admin/classAdd");
        model.addObject("grades", grades);
        // 获取不是班主任的教师
        List<TeacherVO> teachers = teacherService.findTeacherNoHead();
        model.addObject("teachers", teachers);
        model.addObject("editClass", new ClassesVO());
        return model;
    }

    /**
     * 添加班级
     * @param dto 班级信息
     * @return
     */
    @PostMapping(value="/save")
    public String save(ClassDTO dto) {
        classesService.saveClasses(dto);
        return "redirect:/classes/classes";
    }

    /**
     * 获取所有班级
     * @param gradeId 年级编号
     * @param className 班级名称  可用于模糊查询
     * @param classId  班级编号
     * @return
     */
    @GetMapping(value="/classes")
    public ModelAndView getClasses(@RequestParam(value="gradeId", required=false) Integer gradeId,
                                   @RequestParam(value="className", required=false) String className,
                                   @RequestParam(value="classId", required=false) Integer classId) {
        ModelAndView model = new ModelAndView();
        List<ClassesVO> classes = classesService.getClasses(gradeId, className, classId);
        model.setViewName("admin/classes");
        model.addObject("classes", classes);
        return model;
    }

    /**
     * 进入修改班级页面
     * @param classId
     * @return
     */
    @RequestMapping(value="/edit/{classId}", method=RequestMethod.GET)
    public ModelAndView updateClasses(@PathVariable("classId") Integer classId) {
        ModelAndView model = new ModelAndView();
        // 获取年级信息
        List<GradeVO> grades = gradeService.findAllGrade();
        model.setViewName("/admin/classedit");
        model.addObject("grades", grades);
        // 获取不是班主任的教师
        List<TeacherVO> teachers = teacherService.findTeacherNoHead();
        model.addObject("teachers", teachers);
        ClassesVO classesVO = classesService.getClassById(classId);
        model.addObject("editClass", classesVO);
        return model;
    }

    /**
     * 修改班级信息
     * @param dto
     * @return
     */
    @PostMapping(value="/update")
    public String update(ClassDTO dto) {
        classesService.updateClasses(dto);
        return "redirect:/classes/classes";
    }

    /**
     * 删除班级
     * @param classId 班级编号
     * @return
     */
    @PostMapping(value="/del/{classId}")
    public String delClass(@PathVariable("classId") Integer classId) {
        classesService.delClass(classId);
        return "redirect:/classes/classes";
    }


}
